package com.studor.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.studor.common.Result;
import com.studor.dto.LoginRequest;
import com.studor.dto.LoginResponse;
import com.studor.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@RestController
@RequestMapping("/api/auth")
public class AuthController {
    
    @Autowired
    private AccountService accountService;
    
    @PostMapping("/login")
    public Result<LoginResponse> login(@Valid @RequestBody LoginRequest request) {
        try {
            LoginResponse response = accountService.login(request);
            return Result.success("登录成功", response);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @PostMapping("/logout")
    public Result<Void> logout() {
        StpUtil.logout();
        return Result.success("退出登录成功");
    }
    
    @GetMapping("/info")
    public Result<Object> getUserInfo() {
        try {
            Object loginId = StpUtil.getLoginId();
            return Result.success("获取用户信息成功", loginId);
        } catch (Exception e) {
            return Result.error("未登录");
        }
    }
} 